﻿Option Strict On

Imports Sunts.HyperV.Common


Public Class frmBackupJob
    Private Sub BackupJobSetup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillExistingDevices()
        FillExistingServers()
        FillMailRecipients()

        btnSubmitJob.Enabled = True

        Try
            Me.cmbBackupType.DataSource = BackupRecurrenceType.SelectAll(My.Settings.ConnectionString)
            Me.cmbBackupType.DisplayMember = "FriendlyName"
            Me.cmbBackupType.ValueMember = "BackuprecurrenceTypeID"
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnSubmitJob_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmitJob.Click
        Try
            If lstVMs.SelectedItems.Count <> 1 Then
                MessageBox.Show("Select a server to backup")
                Exit Sub
            End If
            If lstBackupDevices.SelectedItems.Count <> 1 Then
                MessageBox.Show("Select a backup device")
                Exit Sub
            End If

            ScheduleJob()
            Me.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


    Private Sub ScheduleJob()
        Dim MailJob As MailJob
        Dim VirtualMachine As VirtualMachine
        Dim BackupJob As BackupJob
        Dim BackupJobSchedule As BackupJobSchedule

        Try

            VirtualMachine = CType(Me.lstVMs.SelectedItems(0).Tag, VirtualMachine)

            BackupJob = New BackupJob
            BackupJob.BackupDeviceID = CType(Me.lstBackupDevices.SelectedItems(0).Tag, BackupDevice).DeviceID
            BackupJob.VirtualMachineID = VirtualMachine.VirtualMachineId
            BackupJob.Deleted = False
            BackupJob.BackupRecurrenceType = CInt(Me.cmbBackupType.SelectedValue)
            BackupJob.ActivationDate = Me.dtpBackupActiveDate.Value.Date
            BackupJob.ExpirationDate = Me.dtpBackupExpirationDate.Value.Date
            BackupJob.BackupTime = Me.dtpBackupTime.Value
            BackupJob.JobName = VirtualMachine.VirtualMachineName
            BackupJob.Retention = 2
            BackupJob.BackupJobID = BackupJob.Insert(My.Settings.ConnectionString)

            BackupJobSchedule = New BackupJobSchedule
            BackupJobSchedule.BackupJobID = BackupJob.BackupJobID
            BackupJobSchedule.Progress = String.Empty
            BackupJobSchedule.ScheduleDateTime = BackupJob.ActivationDate.AddTicks(BackupJob.BackupTime.TimeOfDay.Ticks)
            BackupJobSchedule.Insert(My.Settings.ConnectionString)


            For Each ListViewItem As ListViewItem In Me.lstMailRecipients.Items
                If ListViewItem.Checked Then
                    MailJob = New MailJob
                    MailJob.BackupJobID = BackupJob.BackupJobID
                    MailJob.MailRecipientID = CType(ListViewItem.Tag, MailRecipient).MailRecipientID
                    MailJob.Insert(My.Settings.ConnectionString)
                End If
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub





#Region "Job Setup Page"

    Private Sub FillExistingDevices()
        Try
            lstBackupDevices.Items.Clear()

            For Each BackupDevice As BackupDevice In BackupDevice.SelectAll(My.Settings.ConnectionString)
                Dim DeviceFreeSpace As Long = GetFreeSpace(BackupDevice.DevicePath & "\")
                Dim ItemText As String

                If DeviceFreeSpace > 1024 Then
                    ItemText = BackupDevice.DeviceName & ControlChars.CrLf & System.Math.Round(DeviceFreeSpace / 1024) & "GB Free"
                Else
                    ItemText = BackupDevice.DeviceName & ControlChars.CrLf & DeviceFreeSpace & "MB Free"
                End If

                Dim itm As New ListViewItem(ItemText)
                itm.Tag = BackupDevice
                ' itm.SubItems.AddRange({BackupDevice.DeviceID.ToString, BackupDevice.DeviceName, BackupDevice.DevicePath, DeviceFreeSpace})
                itm.ImageIndex = 3
                itm.ToolTipText = BackupDevice.DevicePath

                lstBackupDevices.Items.Add(itm)
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub FillExistingServers()
        Try
            lstVMs.Items.Clear()
            For Each VirtualMachine As VirtualMachine In VirtualMachine.SelectAll(My.Settings.ConnectionString)
                Me.lstVMs.Items.Add(VirtualMachine.VirtualMachineName, 4).Tag = VirtualMachine
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub FillMailRecipients()
        Dim ListViewItem As ListViewItem
        Try
            lstMailRecipients.Clear()
            For Each MailRecipient As MailRecipient In MailRecipient.SelectAll(My.Settings.ConnectionString)
                ListViewItem = New ListViewItem
                ListViewItem.Text = MailRecipient.Recipient
                ListViewItem.Name = MailRecipient.Recipient
                ListViewItem.Tag = MailRecipient
                lstMailRecipients.Items.Add(ListViewItem)
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try


    End Sub


    Private Sub cmbBackupType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBackupType.SelectedIndexChanged
        If Me.cmbBackupType.Text.ToLower.Contains("recurring") Then
            Me.dtpBackupActiveDate.Enabled = True
            Me.dtpBackupExpirationDate.Enabled = True
        Else
            Me.dtpBackupExpirationDate.Enabled = False
        End If
    End Sub

#End Region



    Private Sub dtpBackupActiveDate_CloseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpBackupActiveDate.CloseUp
        If Me.dtpBackupActiveDate.Value.Date < Now.Date Then
            MessageBox.Show("The backup activation date must not be a date in the past.")
            dtpBackupActiveDate.Value = Now
        End If
    End Sub

End Class